import * as type from '../types'
import Vue from 'vue'
import {
	// _getLoginUser,
	// _getUserInfo,
	// _logout,
	// _postFile,
	// _getTitleAndLogo,
	// _getRouterList,
	_getChangePassword,
	_getMenuByAppId,
	_getAllRouter,
	_getAuthButton
} from '@/service/api/user'
// import  url  from '../../service/config'
// const imgUrl = url.imgUrl

const state = {
	userInfo: {},
	roles: [],
	buttonCanUse:[],
	systems: [],
	logoInfo:{
        appName:'数据管理平台',
        appEnName:'Data Management',
        logPath:''
    }
}

const actions = {
	async fetchChangePassword(config, params = {}) {
        let res = await _getChangePassword(params)
        return res && res.status ? res : false
    },
	async fetchAuthButton({ commit }, params) {
		// let mock = [{"id":3,"routerId":null,"name":"新增","sign":'add'},{"id":5,"routerId":null,"name":"编辑","sign":'edit'}]
		// commit(type.SET_BUTTON,mock)
		// return
		let res = await _getAuthButton(params)
		if(res && res.status) {
			commit(type.SET_BUTTON,res.data)
		}
	},
	async fetchUserInfo({ commit }, params) { // 获取用户信息
		let appId
		if (params && params.appId) {
			appId = params.appId
		} else {
			
			// 暂时注释
			// let userInfo = await _getUserInfo()
			let userInfo = {
				"status": true,
				"code": 0,
				"message": null,
				"data": {
				  "id": null,
				  "userName": "admin",
				  "sn": "wt",
				  "phoneNum": "14878787878",
				  "email": "14774747474@qq.com",
				  "gender": null,
				  "birthday": null,
				  "nativePlace": null,
				  "status": null,
				  "createBy": null,
				  "createDate": null,
				  "updateBy": null,
				  "updateDate": null,
				  "startDate": null,
				  "endDate": null,
				  "password": null,
				  "passwordReset": null,
				  "userType": null,
				  "indexUrl": null
				},
				"otherData": {}
			  }
			if (!userInfo || !userInfo.status) return false
			let system = await _getMenuByAppId(35) // 获取用户的系统
			if(!system) return false
			if (!system.status) return true
			commit(type.SYSTEMS, system.data)
			commit(type.USERINFO, userInfo.data)
			// 通过应用id获取可访问路由
			localStorage.setItem('systemAppId', 35) // 暂时写死
			appId = Vue.prototype.$getQueryObject().appId ? Vue.prototype.$getQueryObject().appId :localStorage.getItem('systemAppId')
			if(appId) {
				localStorage.setItem('systemAppId',appId)
			}else{
				window.location.href = '/'
				return
			}
		}
		// let menuLists = [{"id":2,"appId":2,"name":"roleManage","path":"/main/permissionManagement/roleManage","title":"角色管理","icon":"iconic_jiaoseguanli","children":null},{"id":4,"appId":2,"name":"interfaceManage","path":"/main/permissionManagement/interfaceManage","title":"接口管理","icon":"iconic_jiekouguanli","children":null},{"id":1,"appId":2,"name":"permissionManagement","path":"/main/permissionManagement","title":"权限管理","icon":"iconic_quanxianguanli","children":null},{"id":3,"appId":2,"name":"applicationManage","path":"/main/permissionManagement/applicationManage","title":"应用管理","icon":"iconic_yingyongguanli","children":null},{name:'organization',path:'/'},{name:'organizationManage',path:'/'},{name:'permissionManagement'},{name:'appManage'},{name:'applicationDetail'},{name:'applicationManage'}]
		// commit(type.SET_ROLES, menuLists)
		// return
		let role = await _getAllRouter()
		if(!role) return false
		if (!role.status) return true;
		let menuList = []
		let filterList = (data) => {
			data.map((v)=>{
				menuList.push(v)
				if(v.children){
					filterList(v.children)
				}
			})
		}
		filterList(role.data)
		commit(type.SET_ROLES, menuList)
		// commit(type.SET_ROLES, [{}])
		// 获取logo和名字
		// 暂时注释
		// let logoRes = await _getTitleAndLogo(appId)
		// if (logoRes && logoRes.status && logoRes.data){
		// 	let data = logoRes.data
		// 	if (data.logPath) {
		// 		data.logPath = imgUrl + data.logPath
		// 	}
		// 	commit(type.GET_LOGO_INFO, data)
		// }
		return true
	},
	// async fetchPostFile(config, params) {
	// 	let res = await _postFile(params);
	// 	return res && res.status ? res : false
	// },
	async fetchLogout() { // 登出
		const res = await _logout()
		if (res && res.status) {
			window.location.href = '/'
		}
	},
}

const mutations = {
	[type.GET_LOGO_INFO] (state, data) {
        state.logoInfo = data
  },
	[type.USERINFO](state, data) {
		state.userInfo = data
	},
	[type.SYSTEMS](state, data) {
		state.systems = data
	},
	[type.SET_ROLES] (state, data) {
		state.roles = data
	},
	[type.SET_BUTTON] (state, data) {
		state.buttonCanUse = data
	}
}

export default {
	namespaced: true,
	state,
	actions,
	mutations
}
